from flask_restful import Resource, reqparse,request
from ....Model.Models import  Comment as CommentModel
from ....utils import timestamp,response,verifytoken

from flask_restful import reqparse, abort, Api, Resource, fields,\
    marshal_with
from flask_restful_swagger import swagger

success = 1
notfond = -1
error = 0

token_invalid = 1000


class getCommentlist(Resource):

    def __init__(self, db, auth=None):
        self.db = db

    @swagger.operation(
        notes='获取资讯评论列表',
        responseMessages=[
            {
                "code": 201,
                "message": "Created. The URL of the created blueprint should " +
                "be in the Location header"
            },
            {
                "code": 405,
                "message": "Invalid input"
            }
        ])
    def get(self,information_id,token):
        try:
            if(verifytoken(token)):#验证token值
                #查询获取资讯id的评论数据
                return response(success,None, [each.as_dict() for each in self.db.session.query(CommentModel).filter_by(information_id=information_id).all()])
            return response(token_invalid,None,None)
        except Exception as er:
            return response(error,er,None)

# """
# 评论
# """
class commenting(Resource):
    def __init__(self, db, auth=None):
        self.db = db

    
    @swagger.operation(
      notes='添加评论',
      parameters=[
            {
            "name": "content",
            "description": "评论",
            "dataType": 'string',
            "paramType": "form"
          },{
            "name": "information_id",
            "description": "资讯id",
            "dataType": 'int',
            "paramType": "form"
          },{
            "name": "comment_user_id",
            "description": "评论人id",
            "dataType": 'int',
            "paramType": "form"
          },{
            "name": "create_user_id",
            "description": "资讯创建人id",
            "dataType": 'int',
            "paramType": "form"
          },
          {
            "name": "token",
            "description": "token",
            "dataType": 'int',
            "paramType": "form"
          }
      ],
      responseMessages=[
          {
              "code": 201,
              "message": "Created. The URL of the created blueprint should " +
              "be in the Location header"
          },
          {
              "code": 405,
              "message": "Invalid input"
          }
      ])
    def post(self):#添加
        try:
            args = request.form#从form表单里获取数据
            if(verifytoken(args['token'])):#验证token
                #插入数据
                self.db.session.add(
                        CommentModel(
                            content=args['content'],
                            information_id=args['information_id'],
                            comment_user_id =args['comment_user_id'],
                            create_user_id=args['create_user_id'],
                            update_at=timestamp(),
                            create_at=timestamp()
                        )
                    )
                self.db.session.commit
                return response(success,None,None) #返回状态
            else:
                return response(token_invalid,'登录超时',None)
        except Exception as er:
            return response(error,er,None)



class getCommentListByUserId(Resource):
    def __init__(self, db, auth=None):
        self.db = db

    @swagger.operation(
        notes='获取资讯评论列表',
        responseMessages=[
            {
                "code": 201,
                "message": "Created. The URL of the created blueprint should " +
                "be in the Location header"
            },
            {
                "code": 405,
                "message": "Invalid input"
            }
        ])
    def get(self,user_id,token):
        try:
            if(verifytoken(token)):#验证token值
                #查询获取创建人是user_id的评论
                return response(success,None, [each.as_dict() for each in self.db.session.query(CommentModel).filter_by(create_user_id=user_id).all()])
            return response(token_invalid,None,None)
        except Exception as er:
            return response(error,er,None)
